/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.parts;

import com.eng.scc.core.db.ConfigurationManager;
import com.eng.scc.core.util.HTMLFactory;
import com.eng.scc.core.view.ApplicationContext;
import java.awt.BorderLayout;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import javax.swing.text.html.parser.ParserDelegator;

/**
 *
 * @author Rafael
 */
public class SummaryPane extends JPanel implements HyperlinkListener
{
    public SummaryPane()
    {        
        initComponents();
    }

    public void setContent( HTMLFactory.Body body )
    {
        try
        {
            body.setUrl( urlCSS.toString() );
            doc.setInnerHTML( doc.getDefaultRootElement(), body.toString() );
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    private void initComponents()
    {
        loadStyles();
        
        editorPane.setContentType( "text/html"); 
        editorPane.setEditable( false );
        editorPane.setEditorKit( kit );
        
        scrollPane.setViewportView( editorPane );
        setLayout( new BorderLayout() );
        
        doc = new HTMLDocument( kit.getStyleSheet() );
        doc.setParser( new ParserDelegator() );
        editorPane.setDocument( doc );
        editorPane.addHyperlinkListener( this );
        
        add( BorderLayout.CENTER, scrollPane );
    }
    
    public void loadStyles()
    {
        try
        {
            StyleSheet styles = new StyleSheet();
            
            File file = new File( ConfigurationManager.getInstance().getHomePath() + "css/default.css" );
            
            FileReader reader = new FileReader( file );
            styles.loadRules( reader, urlCSS = file.toURI().toURL() );
            reader.close();
            kit.setStyleSheet( styles );
        }

        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    private URL urlCSS = null;
    
    private JScrollPane scrollPane = new JScrollPane();
    private JEditorPane editorPane = new JEditorPane();
    private HTMLEditorKit kit = new HTMLEditorKit();
    
    private HTMLDocument doc = null;
    
    public static void main( String[] args )
    {
        JFrame frame = new JFrame( "teste" );
        frame.setSize( 800, 600);
        SummaryPane productsTree = new SummaryPane();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( productsTree );
        frame.setVisible( true );
    }

    @Override
    public void hyperlinkUpdate( HyperlinkEvent e )
    {
        if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED )
        {
            fireStateChanged( e.getDescription() );
        }
    }
    
    private void fireStateChanged( String s )
    {
        for ( ChangeListener ce : changeListener )
        {
            ce.stateChanged( new ChangeEvent( s ) );
        }
    }
    
    public void addChangeListener( ChangeListener ce )
    {
        changeListener.add(ce);
    }
    
    private List<ChangeListener> changeListener = new ArrayList<ChangeListener>();
}
